//
//  WLHomeHeaderView.swift
//  LianTongSF
//
//  Created by Visitor on 2017/7/7.
//  Copyright © 2017年 万利. All rights reserved.
//

import UIKit

class WLHomeHeaderView: UIView {

    @IBOutlet weak var userIcon: UIImageView!
    @IBOutlet weak var moneyLabel: UILabel!
    @IBOutlet weak var flowVIew: UIView!
    @IBOutlet weak var phoneNumLabel: UILabel!
    @IBOutlet weak var mesLabel: UILabel!
    @IBOutlet weak var netLabel: UILabel!
      @IBOutlet weak var personScrollView: UIView!
    
    var dataAry: NSMutableArray = []
    
    @IBAction func payFlowBtnClick() {
        
    }
    
    override func awakeFromNib() {
        userIcon.layer.cornerRadius = userIcon.frame.width / 2
        userIcon.layer.masksToBounds = true
        userIcon.layer.borderColor = UIColor.white.cgColor
        userIcon.layer.borderWidth = 0.5
        
        moneyLabel.text = "3.55"
        phoneNumLabel.text = "5"
        mesLabel.text = "10"
        netLabel.text = "25"
        
        let path = Bundle.main.path(forResource: "menuData", ofType: "plist")
        self.dataAry = NSMutableArray(contentsOfFile: path!)!
        DLog(log: self.dataAry)
        personScrollView.frame.size.width = M_S.width
        personScrollView.addSubview(collectionView)
        personScrollView.addSubview(pageControl)
        
        progressView.frame = CGRect(x: 0, y: 0, width: M_S.width - 129, height: 15)
        progressView.progress = 0.85
        flowVIew.addSubview(progressView)
        
    }
    
    private lazy var progressView: WLCustormProgressView = {
        let loadingView = WLCustormProgressView()
        loadingView.color = RGB(r: 115, g: 185, b: 254)
        loadingView.borderRadius = 5
        loadingView.animate = 1
        loadingView.flat = 1
        loadingView.showText = 0
        loadingView.type = WLProgressSolid
        return loadingView
    }()
    
    
    private lazy var collectionView: UICollectionView = {
        let layout = WLFlowLayout(rowCount: 1, itemCountPerRow: 4)!
        layout.itemSize = CGSize(width: CGFloat(M_S.width / 4), height: 79)
        layout.scrollDirection = .horizontal
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        
        let col = UICollectionView(frame: CGRect(x: 0, y: 0, width: M_S.width, height: 79), collectionViewLayout: layout)
        col.register(WLCollectionViewCell.self, forCellWithReuseIdentifier: "reuseWaterCollectionCell")
        col.isPagingEnabled = true
        col.delegate = self
        col.dataSource = self
        col.backgroundColor = .white
        col.alwaysBounceVertical = false
        col.alwaysBounceHorizontal = true
        col.showsHorizontalScrollIndicator = true
        return col
    }()

    
     lazy var pageControl: UIPageControl = {
        let page = UIPageControl()
        page.frame = CGRect(x: 15, y: self.collectionView.frame.maxY, width: M_S.width - 30, height: 15)
        page.backgroundColor = .clear
        page.currentPage = 0
        page.numberOfPages  = (self.dataAry.count + 1) % 4 == 0 ?  (self.dataAry.count + 1) / 4 : (self.dataAry.count + 1 ) / 4 + 1;
//        page.setValue([UIImage (named: "compose_keyboard_dot_normal")], forKeyPath: "pageImage")
//        page.setValue(UIImage(named:"compose_keyboard_dot_selected"), forKeyPath: "currentPageImage")
        page.currentPageIndicatorTintColor = .orange
        page.pageIndicatorTintColor = UIColor.lightGray
        page.hidesForSinglePage = true
        return page
    }()
  
    
}

extension WLHomeHeaderView: UICollectionViewDelegate, UICollectionViewDataSource{

    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return dataAry.count + 1
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "reuseWaterCollectionCell", for: indexPath) as! WLCollectionViewCell

        cell.dataDict =  indexPath.item  <= (self.dataAry.count - 1 ) ? (self.dataAry[indexPath.item] as! [String: String]) : nil
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
     
    }
    
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
//        let page = Int(scrollView.contentOffset.x / scrollView.frame.size.width)
//        self.pageControl.currentPage = page
        let pageNo = scrollView.contentOffset.x / scrollView.frame.size.width;
        self.pageControl.currentPage = Int(pageNo + 0.5);
    }

}
